require 'common_settings.rb'
require 'common_lib.rb'

def init
  $model.start_operation "clear_op"
  $entities.clear!
  puts "clearing "+$model.definitions.length.to_s+" components"
  $model.definitions.purge_unused    
  $model.commit_operation
  puts "remaining "+$model.definitions.length.to_s+" components"
  
  $materials.purge_unused
  
  load_materials
  
  #view = $model.active_view
  #status = view.refreshSketchUp
  
  begin
    build_home
    
    puts "finish"
    return "OK"
    
  rescue Exception => e
    puts e.inspect
    puts e.backtrace
    raise "some errors seen: " + e
  end
  
end

def load_materials
  # load materials
  files = Dir.glob( $project_home+File::Separator+"img"+File::Separator+"*.jpg" )
  puts "loading "+files.length.to_s+" materials"
  files.each { |file| 
    puts "creating material from file #{file}";
    material_name = file.gsub /.*\//, ''
    material_name = material_name.gsub /\.jpg/, ''
    puts "material name "+material_name
    material = $materials.add material_name
    material.texture = file
  }
end